package com.chengqj.factory.singlefactory;

/**
 * @program: pattern
 * @description: 懒汉式方式二：内部类方式
 * @author: chengqj
 * @create: 2018-08-06 18:16
 **/
public class LazyTwo {
    private boolean initialized = false;

    private LazyTwo() {
        synchronized (LazyTwo.class) {
            if (initialized == false) {
                initialized = !initialized;
            } else {
                throw new RuntimeException("单例已被侵犯");
            }
        }
    }

    public static final LazyTwo getInstance() {
        return LazyHolder.LAZY;
    }

    private static class LazyHolder {
        private static final LazyTwo LAZY = new LazyTwo();
    }
}
